package com.zhxh.nzpos.sell;

import java.util.ArrayList;

import android.app.Activity;
import android.content.SharedPreferences;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhxh.nzpos.R;
import com.zhxh.nzpos.goods.GoodsItemCallBack;
import com.zhxh.nzpos.model.Goods;

public class GoodsSelectListAdapter extends BaseAdapter {

	private ArrayList<Goods> items;
	private Activity activity;
	private GoodsItemCallBack deleteCallback;
	public static final String PREFS_NAME = "PreferencesFile";
	Goods item;

	public GoodsSelectListAdapter(Activity activity,
			ArrayList<Goods> dealItems, GoodsItemCallBack deleteCallback) {
		this.items = dealItems;
		this.activity = activity;
		this.deleteCallback = deleteCallback;
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	public ArrayList<Goods> getItems() {
		// TODO Auto-generated method stub
		return items;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		item = items.get(position);
		System.out.println("position==" + position);
		if (convertView == null) {
			convertView = activity.getLayoutInflater().inflate(
					R.layout.goods_select_list_item, null);
			holder = new ViewHolder();
			// holder.identifier = (TextView)
			// convertView.findViewById(R.id.goods_identifier_et);
			holder.name = (TextView) convertView
					.findViewById(R.id.goods_name_et);
			holder.specification = (TextView) convertView
					.findViewById(R.id.goods_specification_et);
			holder.cBox = (CheckBox) convertView.findViewById(R.id.cb);

			holder.cBox
					.setOnCheckedChangeListener(new OnCheckedChangeListener() {

						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							//商品选择后添加到prefercense
							if (buttonView.isChecked()) {
								SharedPreferences settings = activity
										.getSharedPreferences(PREFS_NAME, 0);

								String products = settings.getString(
										"prodcuts", null);
								if (products != null) {
									products += "," + buttonView.getTag();
								} else {
									products = (String) buttonView.getTag();
								}
								SharedPreferences.Editor editor = settings
										.edit();
								editor.putString("prodcuts", products);
								editor.commit();
							} else { //商品删除后，从reference中去掉
								SharedPreferences settings = activity
										.getSharedPreferences(PREFS_NAME, 0);

								String products = settings.getString(
										"prodcuts", null);
								if(products != null) {
									String goodid = (String) buttonView.getTag();
									if(products.indexOf(goodid) > -1) {
										products = products.replace(goodid, "");
									}
									if(products.length() > 0) {
										if(products.startsWith(",")) {
											products = products.substring(1);
										}
										else if(products.endsWith(",")) {
											products = products.substring(0,products.length()-1);
										}
										else if(products.indexOf(",,") > -1) {
											products = products.replace(",,",",");
										}
									}
									SharedPreferences.Editor editor = settings
											.edit();
									editor.putString("prodcuts", products);
									editor.commit();
								}
								
							}
						}
					});
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if (item != null) {
			// holder.identifier.setText(item.getId());
			holder.name.setText(item.getName());
			holder.specification.setText("" + item.getSpecification());
			holder.cBox.setTag(item.getId());
			SharedPreferences settings = activity
					.getSharedPreferences(PREFS_NAME, 0);

			String products = settings.getString(
					"prodcuts", null);
			if(products != null) {
				if(products.contains(item.getId())) {
					holder.cBox.setChecked(true);
				}
			}
			// holder.category.setText(""+item.getCategoryName());
			/*
			 * holder.price.setText(""+item.getPrice1());
			 * holder.unit.setText(""+item.getUnits1());
			 * 
			 * holder.price2.setText(""+item.getPrice2());
			 * holder.unit2.setText(""+item.getUnits2()); //
			 * holder.code.setText(""+item.getCode());
			 * holder.points.setText(""+item.getPoints());
			 */
		}
		return convertView;
	}

	static class ViewHolder {
		TextView identifier;
		TextView name;
		TextView specification;
		// TextView code;
		TextView points;
		ImageView modifyBtn;
		CheckBox cBox;
	}
}
